﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace YJYpublicClass
{

    public class SD2_24W124 
    {
        

        private SerialPort sport = new SerialPort();

        private object syncRoot = new object();                     

        public bool Connection => sport.IsOpen;         //是否已连接



        public void Open(int port, int baudRate, StopBits stopBits, int dataBits, int Timeout)
        {
            lock (syncRoot)
            {
                try
                {
                    if (sport.IsOpen)
                    {
                        sport.Close();
                    }
                    sport.PortName = "COM" + port;
                    sport.BaudRate = baudRate;
                    sport.StopBits = stopBits;
                    sport.DataBits = dataBits;
                    sport.ReadTimeout = Timeout;
                    sport.WriteTimeout = Timeout;
                    sport.Open();
                    sport.ReadExisting();
                }
                catch (Exception ex)
                {
                    string ex1 = ex.ToString();
                }
            }
        }

        public void Close()
        {
            lock (syncRoot)
            {
                sport.Close();
            }
        }

        private bool SendChar(char ch)
        {
            sport.Write(new char[1] { ch }, 0, 1);
            char rch = (char)sport.ReadChar();
            return ch == rch;
        }

        private bool SendCommand(string cmd,out string ReCmd)
        {
            sport.WriteLine(cmd);
            Thread.Sleep(50);
            int n = sport.BytesToRead;
            Byte[] BUF = new byte[n];
            sport.Read(BUF, 0, n);
            ReCmd = System.Text.Encoding.UTF8.GetString(BUF);
            return true;
        }

        protected string ExecuteCommand(string cmd)
        {
            lock (syncRoot)
            {
                string Recmd = string.Empty;
                if (SendCommand(cmd, out Recmd))
                {                    
                    return Recmd.Trim().ToUpper();
                }
                return string.Empty;
            }
        }

        protected int GetIntegerValue(string cmd)
        {
            string ret = ExecuteCommand(cmd);
            return int.Parse(ret);
        }

        protected bool ExecuteAndCheckOk(string cmd)
        {
            string ret = ExecuteCommand(cmd);
            return ret.Equals("SUCCESD");
        }

        public int[] GetValue()
        {
            int[] vals = new int[8];
            string Cmd = "SA0000TB0000TC0000TD0000TE0000TF0000TG0000TH0000TR#";
            string ret = ExecuteCommand(Cmd);
            if (ret.IndexOf('#') > 0)
            {
         
                string[] retArr = ret.Split('T');
                vals[0] = int.Parse(retArr[0].Substring(2, 4));
                vals[1] = int.Parse(retArr[1].Substring(1, 4));
                vals[2] = int.Parse(retArr[2].Substring(1, 4));
                vals[3] = int.Parse(retArr[3].Substring(1, 4));
                vals[4] = int.Parse(retArr[4].Substring(1, 4));
                vals[5] = int.Parse(retArr[5].Substring(1, 4));
                vals[6] = int.Parse(retArr[6].Substring(1, 4));
                vals[7] = int.Parse(retArr[7].Substring(1, 4));
            }
            return vals;
        }

        public int GetValue(int chNo)
        {
            int[] vals = GetValue();
            if (chNo > 3)
            {
                return 0;
            }
            return vals[chNo];
        }

        public bool SetValue(int chNo, int val)
        {
           
            byte[] btNumber = new byte[] { (byte)(chNo+65) };
            System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
            string ChChar= asciiEncoding.GetString(btNumber);
            string cmd = "S" + ChChar + val.ToString("0000") + "TC#";
            return ExecuteAndCheckOk(cmd);
        }

        public bool SetValues(int[] channelArray, int[] values)
        {
            if (channelArray.Length != values.Length || channelArray.Length == 0 || values.Length == 0)
            {
                return false;
            }
            string cmd = "S";
            System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
            for (int i = 0; i < channelArray.Length; i++)
            {
                byte[] btNumber = new byte[] { (byte)(channelArray[i] + 65) };               
                string ChChar = asciiEncoding.GetString(btNumber);
                cmd += ChChar;
                cmd += values[i].ToString("0000");
                cmd += "T";
            }
            cmd += "C#";
            return ExecuteAndCheckOk(cmd);
        }

        public bool IsExists()
        {
            if (!sport.IsOpen)
            {
                return false;
            }
            return true;
        }

        
    }

}
